<%--suppress JSJQueryEfficiency --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/icon.css">
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/color.css">
    <script type="text/javascript"
            src="../../../js/jquery-easyui-1.9.5/jquery.min.js"></script>
    <script type="text/javascript"
            src="../../../js/jquery-easyui-1.9.5/jquery.easyui.min.js"></script>
</head>
<body>
<table id="reader_dg" title="读者信息" class="easyui-datagrid" style="width:100%;height:100%"
       url="../readerMessage/list"
       toolbar="#reader_toolbar"
       rownumbers="true" pagination="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="readerNo" style="width: 12.5%">读者编号</th>
        <th field="name" style="width: 12.5%">读者姓名账号</th>
        <th field="password" style="width: 12.5%">密码</th>
        <th field="readerSex" style="width: 12.5%">性别</th>
        <th field="readerPhone" style="width: 12.5%">联系方式</th>
        <th field="readerEmail" style="width: 12.5%">电子邮箱</th>
        <th field="readerRegister" style="width: 12.5%">注册时间</th>
        <th field="dept" formatter="deptFormatter" style="width: 12.5%">所属部门</th>
    </tr>
    </thead>
</table>

<div id="reader_toolbar">
    <label for="searchNameReaderMessage">读者姓名账号:</label>
    <input id="searchNameReaderMessage" name="name" type="text" class="easyui-textbox"/>
    <label for="searchDeptReaderMessage">部门编号:</label>
    <input id="searchDeptReaderMessage" name="dept.deptNo"
           class="easyui-combobox" data-options="valueField:'deptNo',textField:'deptName',
                   url:'../readerMessage/deptList'">
    <a id="searchButton" href="javascript:doReaderMessageSearch();" class="easyui-linkbutton"
       data-options="iconCls:'icon-search'">Search</a>
    <a id="resetButton" href="javascript:doReaderMessageReset();" class="easyui-linkbutton"
       data-options="iconCls:'icon-redo'">Reset</a>
    <br/>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newReaderMessage()">New
        ReaderMessage</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editReaderMessage()">Edit
        ReaderMessage</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyReaderMessage()">Remove
        ReaderMessage</a>
</div>

<%--        新增和修改对话框--%>
<div id="reader_dlg" class="easyui-dialog" style="width:400px;height:300px;padding:10px 20px"
     closed="true" buttons="#reader_dlg-buttons">
    <div class="ftitle" style="text-align: center">Reader Message</div>
    <br>
    <form id="reader_fm" method="post">
        <div class="fitem">
            <label for="name">读者账号&emsp;</label>
            <input id="name" name="name" class="easyui-textbox" prompt="name" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="password">密&emsp;&emsp;码&emsp;</label>
            <input id="password" name="password" class="easyui-passwordbox" prompt="password" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="readerSex">性&emsp;&emsp;别&emsp;</label>
            <input id="readerSex" name="readerSex" class="easyui-textbox" prompt="readerSex" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="readerPhone">联系方式&emsp;</label>
            <input id="readerPhone" name="readerPhone" class="easyui-textbox" prompt="readerPhone" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="readerEmail">电子邮箱&emsp;</label>
            <input id="readerEmail" name="readerEmail" class="easyui-textbox" prompt="readerEmail" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="readerRegister">注册时间&emsp;</label>
            <input id="readerRegister" name="readerRegister" class="easyui-datetimebox"
                   data-options="showSeconds:true,formatter:formatter,parser:parser" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="dept">所属部门&emsp;</label>
            <input id="dept" name="dept.deptNo" class="easyui-combobox"
                   data-options="required:true,missingMessage:'deptNo is not null',
                           valueField:'deptNo',textField:'deptName',url:'../readerMessage/deptList'"
                   style="width: 250px">
        </div>
    </form>
</div>

<div id="reader_dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveReader()">Save</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="$('#reader_dlg').dialog('close')">Cancel</a>
</div>

<script type="text/javascript">
    //框架进阶：1.能使用框架，2.能扩展框架，3.能修改框架，4.能设计框架
    //修改easyUI组件的时间格式-时间格式化器-把时间对象转换为字符串
    function formatter(date) {
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hour = date.getHours().toString();
        let minutes = date.getMinutes().toString();
        let seconds = date.getSeconds().toString();
        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;
        hour = hour < 10 ? '0' + hour : hour;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        seconds = seconds < 10 ? '0' + seconds : seconds;
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
    }

    //时间解析器：把字符串转换为javascript的时间对象
    function parser(s) {
        let t = Date.parse(s);
        if (!isNaN(t)) {
            return new Date(t);
        } else {
            return new Date(s + ":00:00");
        }
    }

    function deptFormatter(value) {
        return value.deptName;
    }

    function doReaderMessageSearch() {
        $("#searchNameReaderMessage").textbox('setValue', $("#searchNameReaderMessage").val().trim());
        //调用datagrid对象的load方法查询数据，并传递查询参数
        $("#reader_dg").datagrid("load", {
            "name": $("#searchNameReaderMessage").val(),
            "dept.deptNo": $("#searchDeptReaderMessage").val()
        });
    }

    function doReaderMessageReset() {
        $("#searchNameReaderMessage").textbox('setValue', '');
        $("#searchDeptReaderMessage").combobox('clear');
        //清空查询参数后，重新加载数据
        $("#reader_dg").datagrid("load", {});
    }

    function loadDeptSuccess(value) {
        $('#dept').combobox({
            onLoadSuccess: function (data) {
                if (data) {
                    if (value) {
                        $('#dept').combobox('select', value);
                    } else {
                        $('#dept').combobox('select', data[0].deptNo);
                    }
                }
            }
        });
    }

    function newReaderMessage() {
        $('#reader_dlg').dialog('open').dialog('setTitle', 'New ReaderMessage');
        $('#reader_fm').form('clear');
        url = '../readerMessage/add';
        const d = new Date();
        const nowDate = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() +
            " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        $('#readerRegister').datetimebox('setValue', nowDate);
        loadDeptSuccess();
    }

    function editReaderMessage() {
        $('#reader_fm').form('clear');
        var row = $('#reader_dg').datagrid('getSelected');
        if (row) {
            $('#reader_dlg').dialog('open').dialog('setTitle', 'Edit reader');
            $('#reader_fm').form('load', row);
            url = '../readerMessage/edit?readerNo=' + row.readerNo;
            loadDeptSuccess(row.dept.deptNo);
        } else {
            $.messager.show({
                title: 'Error',
                msg: "请选择要修改的用户信息"
            });
        }
    }

    function destroyReaderMessage() {
        var row = $('#reader_dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', '是否需要删除该信息？', function (r) {
                if (r) {
                    $.post('../readerMessage/delete', {readerNo: row.readerNo}, function (result) {
                        if (result.success) {
                            $('#reader_dg').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.show({
                title: 'Error',
                msg: "请选择要删除的用户信息"
            });
        }
    }

    function saveReader() {
        $('#reader_fm').form('submit', {
            url: url,
            onSubmit: function () {
                // 提交表单前 表单验证
                return $(this).form('validate');
            },
            success: function (result) {
                // 收到服务器响应的json字符串后，调用这里的代码，回调函数当浏览器收到服务器返回的数据后才执行
                var result = eval('(' + result + ')');
                if (!result.success) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                } else {
                    $('#reader_dlg').dialog('close');		// 新增成功，关闭对话框
                    $('#reader_dg').datagrid('reload');	// 新增成功，刷新列表数据
                }
            }
        });
    }
</script>
</body>
</html>
